import {i18n} from '../locale/index';
const { t } = i18n.global;

export function getTaskResult(task) {
	if(task.isFuture) return {text: t('webAuto.Waiting'), type: 'info'}
	const result = task.result || task.taskResult
	if (result === 'agree') {
		return {
			text: t('webAuto.Agreed'),
			type: 'success'
		}
	} else if (result === 'complete') {
		return {
			text: t('webAuto.AlreadyProcessed'),
			type: 'success'
		}
	} else if (result === 'refuse') {
		return {
			text: t('webAuto.REJECTED'),
			type: 'error'
		}
	} else if (result === 'transfer') {
		return {
			text: t('webAuto.Transferred'),
			type: 'primary'
		}
	} else if (result === 'recall') {
		return {
			text: t('webAuto.RETURNED'),
			type: 'warning'
		}
	} else if (task.result === 'retrieval') {
		return {
			text: t('webAuto.retrieval'),
			type: 'default'
		}
	} if (!result && task.finishTime) {
		return {
			text: t('webAuto.Cancelled'),
			type: 'default'
		}
	}else {
		return {
			text: t('webAuto.Processing'),
			type: 'primary'
		}
	}
}

export function getProcTag(result) {
	switch (result) {
		case 'RUNNING':
			return {
				text: t('flow.inProgress'), type: 'primary'
			}
		case 'COMPLETE':
			return {
				text: t('webAuto.StatusEnded'), type: 'primary'
			}
		case 'PASS':
			return {
				text:t('webAuto.Approved'), type: 'success'
			}
		case 'CANCEL':
			return {
				text: t('webAuto.revoked'), type: 'default'
			}
		case 'REFUSE':
			return {
				text: t('webAuto.ApprovalRejected'), type: 'error'
			}
		default: 
			return {
				text: t('webAuto.UnknownState'), type: 'default'
			}

	}
}


//判断是否为主要业务节点
export function isPrimaryNode(node){
  return node && node.type && !(isBranchNode(node) || isEmptyNode(node));
}
export function isBranchNode(node){
  return node && (node.type === 'CONDITIONS' || node.type === 'CONCURRENTS' || node.type === 'INCLUSIVES');
}
export function isEmptyNode(node){
  return node && (node.type === 'EMPTY')
}
//是分支节点
export function isConditionNode(node){
  return node.type === 'CONDITIONS';
}
//是分支节点
export function isBranchSubNode(node){
  return node && (node.type === 'CONDITION' || node.type === 'CONCURRENT' || node.type === 'INCLUSIVE');
}
export function isConcurrentNode(node){
  return node.type === 'CONCURRENTS'
}

export function isInclusiveNode(node){
  return node.type === 'INCLUSIVES'
}

export function forEachNode(node, callback){
  if (isBranchNode(node)){
    if (callback(node)){return}
    node.branchs.map(branchNode => {
      if (callback(branchNode)){return}
      forEachNode(branchNode.children, callback)
    })
    forEachNode(node.children, callback)
  }else if (isPrimaryNode(node) || isEmptyNode(node) || isBranchSubNode(node)){
    if (callback(node)){return}
    forEachNode(node.children, callback)
  }
}